package datastructure.book.dp._2_linear_pre_section.single.all;

import org.junit.jupiter.api.Test;

public class _20_Solution {

    public int minCut(String s) {
        int[] dp = new int[s.length()];
        dp[0] = 1;
        for (int i = 1; i < s.length(); i++) {
            if (isHuiwen(0,i,s)) {
                dp[i] = 1;
                continue;
            }
            dp[i] = dp[i-1]+1;
            for (int j = 1; j < i; j++) {
                if (isHuiwen(j,i,s)) {
                    dp[i] = Math.min(dp[i],dp[j-1]+1);
                }
            }
        }
        return dp[dp.length-1]-1;
    }

    private boolean isHuiwen(int start, int end, String s) {
        while (start < end) {
            if (s.charAt(start) != s.charAt(end)) {
                return false;
            }
            start++;end--;
        }
        return true;
    }

    @Test
    public void test(){
        String str = "babba";
        System.out.println(minCut(str));
    }
}
